﻿using System.Windows;
using System.Windows.Media;
using Microsoft.Phone.Controls;
using _1906Ultras.ViewModel;

namespace _1906Ultras
{
    /// <summary>
    /// Description for PhotoViewer.
    /// </summary>
    public partial class PhotoViewer : PhoneApplicationPage
    {

        private double initialAngle = 0;
        private double initialScale = 0;

        
        /// <summary>
        /// Initializes a new instance of the PhotoViewer class.
        /// </summary>
        public PhotoViewer()
        {
            InitializeComponent();
            Loaded += PageLoaded;

        }


        private void PageLoaded(object sender, RoutedEventArgs e)
        {

            string url;
            if (!NavigationContext.QueryString.TryGetValue("url", out url)) return;

            var photos = ((PhotoViewerViewModel)photoPivot.DataContext).PhotoAlbum;

            //progressBar.IsIndeterminate = true;
            for (int i = 0; i < photos.Count; i++)
            {
                if (photos[i].PhotoLink == url)
                {
                    photoPivot.SelectedIndex = i;
                    break;
                }
            }
            //progressBar.IsIndeterminate = false;
        }

        private void OnPinchStarted(object sender, PinchStartedGestureEventArgs e)
        {
            var image = sender as System.Windows.Controls.Image;
            if (image == null) return;
            var transform = image.RenderTransform as CompositeTransform;
            if (transform == null) return;
            //initialAngle =  transform.Rotation;
            initialScale = transform.ScaleX;
        }

        private void OnPinchDelta(object sender, PinchGestureEventArgs e)
        {
            //ImageScaling.ScaleX = e.DistanceRatio;
            //   ImageScaling.ScaleY = e.DistanceRatio;
            var image = sender as System.Windows.Controls.Image;
            if (image == null) return;
            var transform = image.RenderTransform as CompositeTransform;
            if (transform == null) return;

            //transform.Rotation = initialAngle + e.TotalAngleDelta;
            transform.ScaleX = initialScale * e.DistanceRatio;
            transform.ScaleY = initialScale * e.DistanceRatio;
        }
    }
}